﻿using KInterfaces;
using KWindow.KPageCore;
using KXmlUIControl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IPCPages
{
    public class ProcessStartup : PageProcessBase<ProcessStartup>
    {
        protected List<XmlProgramObject> Programs = null;

        private Starter _starter = null;

        public AppsStartupResult Result
        {
            get
            {
                if (_starter == null)
                {
                    return AppsStartupResult.FatalError;
                }
                return _starter.StartupResult;
            }
        }

        private ProcessStartup() : base()
        {

        }

        private void StartStarter()
        {
            PageStartup pageStartup = MainPage as PageStartup;
            _starter = new Starter(MainPage, Programs);
            _starter.ProgramStatusUpdated += pageStartup.UpdateGridRowStatus;
            _starter.StartupFinished += pageStartup.OnStartupFinished;
            _starter.Init();
            Win32API.ShowTaskBar(true);
        }

        public override void Init()
        {
            base.Init();
        }

        public override void Exit()
        {
            base.Exit();
            if (_starter != null)
            {
                _starter.OnAppExited(MainPage.Handle);
            }
        }

        public override void InitData()
        {
            base.InitData();
            Programs = ShareDataManager.GlobalData.GetValue<List<XmlProgramObject>>("Programs");
        }

        public override void UpdateData()
        {
            base.UpdateData();
        }

        public override void StartWork()
        {
            base.StartWork();
            if (_starter == null)
            {
                StartStarter();
            }
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        public override void PlayWelcomeVoice()
        {            
            base.PlayWelcomeVoice();
        }
    }
}
